<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>
        <input type="text" id="uname"> <input type="checkbox" id="chk"> 保存
    </p>

    <script>
        // 获取元素
        const uname = document.querySelector('#uname');
        const chk = document.querySelector('#chk');

        if (!!window.localStorage.getItem('uname')) {
            uname.value = window.localStorage.getItem('uname');
            chk.checked = true;
        }

        chk.addEventListener('click',function() {
            if (chk.checked)
                window.localStorage.setItem('uname', uname.value);
            else
                window.localStorage.removeItem('uname');
        });
    </script>
</body>
</html>